package torres.controller;

import torres.RunGame;
import torres.entity.Avatar;
import torres.entity.Entity;
import torres.map.Location;
import torres.map.Map;
import torres.map.World;


/**
 * @author Torres
 * A utility class that allows commands to be issued to Entities.
 */
public class Command {
	
	/**
	 * The method allows the Entity to move the direction passed in.
	 * @param d the direction the entity will move
	 * @param e the entity that wants to move
	 */
	public static void moveEntity(Direction d, Entity e) {
		
		if(RunGame.moveCounter==0) {
		RunGame.moveCounter += 35-e.getMovement();
		Map map = World.getInstance().getCurrentMap();
		Location oldLocation = e.getLocation();
		int x = oldLocation.getX();
		int y = oldLocation.getY();
		Location newLocation = null;
				
		switch(d) {
		case NORTH: newLocation = new Location(x, y-1); break;
		case NORTHWEST: newLocation = new Location(x-1, y-1); break;
		case WEST: newLocation = new Location(x-1, y); break;
		case SOUTHWEST: newLocation = new Location(x-1, y+1); break;
 		case SOUTH: newLocation = new Location(x, y+1); break;
		case SOUTHEAST: newLocation = new Location(x+1, y+1); break;
		case EAST: newLocation = new Location(x+1, y); break;
		case NORTHEAST: newLocation = new Location(x+1, y-1); break;
		}
		
		boolean test = e.move(newLocation);
		
		if (map.getAvatar().getLevel() > 49) {
			map.getAvatar().setImageName("dragon" + e.getDirection() + ".png"); 
		}
		else {
			map.getAvatar().setImageName("llama" + e.getDirection()+ ".png"); 
		}
		
		if(test) {
		
			map.removeEntity(oldLocation);
			map = World.getInstance().getCurrentMap();
			map.addEntity(e.getLocation(), e);
		}
		}
	}
	/**
	 * This method adds experience to the entity passed in
	 * @param e the entity that will be given experience.
	 */
	public static void addExperience(Entity e) {
		
		int prevExp = e.getExperience();
		if(e.getLevel()<99)
			e.setExperience(prevExp + 100);
	}
	
	/**
	 * Teleport to a different map
	 * @param a the avatar that will teleport
	 * @param map the new map in which to teleport to
	 * @param toLocation the new location that will teleport the avatar towards
	 */
	public static void teleport(Avatar a, Map map, Location toLocation) {
	
		World.getInstance().getCurrentMap().removeAvatar();
		World.getInstance().setCurrentMap(map);
		World.getInstance().getCurrentMap().setAvatar(a);
		a.setLocation(toLocation);
	}
}